#!usr/bin/env python
# -*- coding:utf-8 -*-
"""
数据源：巨潮 -> 市场资讯 -> 上市公司 -> 公司资讯 -> 发行筹资
爬取类目：发行筹资(不包含: 招股说明书、上市公告书、配股说明书)
股票类型不包括港股（港股没有发行筹资的资料）
url_sample: http://www.cninfo.com.cn/information/issue/szmb000001.html
"""

import sys

from pymongo import MongoClient
from company_crawler import CompanyCrawler
from company_crawler import CompanyInfoUtil
from xtls.logger import get_logger
from xtls.timeparser import now
from bs4 import BeautifulSoup
"""
result_sample_of_company_issue = {
    '_id': '',
    'stockCode': '',
    'companyShortName': '',
    'stockType': '',
    'issue': {
        'issueType',
        'issueStartDate',
        'issueProperty',
        'issueStockType',
        'releaseMode',
        'issuePublicNumber',
        'issuePriceRMB',
        'issuePriceForeign',
        'issueFundRaised',
        'issueCost',
        'PERatio',
        'expectedRatioOL',
        'expectedRatioSM',
    }
}
"""

reload(sys)
sys.setdefaultencoding('utf-8')
MONGO = MongoClient('192.168.31.114', 27017)

CATEGORY = 'issue'

DETAIL_MAPPING = {
    u'发行类型：': 'issueType',
    u'发行起始日：': 'issueStartDate',
    u'发行性质：': 'issueProperty',
    u'发行股票种类：': 'issueStockType',
    u'发行方式：': 'releaseMode',
    u'发行公众股数量(万股)：': 'issuePublicNumber',
    u'人民币发行价格(元)：': 'issuePriceRMB',
    u'外币发行价格(元)：': 'issuePriceForeign',
    u'实际募集资金(万元)：': 'issueFundRaised',
    u'实际发行费用(万元)：': 'issueCost',
    u'发行市盈率(倍)：': 'PERatio',
    u'上网定价中签率(%)：': 'expectedRatioOL',
    u'二级配售中签率(%)：': 'expectedRatioSM',
}

class CompanyIssue(CompanyCrawler):
    def __init__(self, company_info_util_instance):
        super(CompanyIssue, self).__init__(company_info_util_instance=company_info_util_instance)
        self.detail_mapping = self.company_info.detail_mapping

    def find_company_info_from(self, url):
        result = {}
        issue_html = self.get(url)
        soup = BeautifulSoup(issue_html, 'html5lib')
        if not soup.find('img', src='/error.jpg'):
            result.update(self._find_issue_table(soup.select('div.clear > table')[0]))
        else:
            self.logger.info(u'undealed: not find the company %s' % url)
        return result

    def _find_issue_table(self, soup):
        issue_table = {}
        for tr in soup.find_all('tr'):
            tds = tr.find_all('td')
            if len(tds) != 2:
                continue
            title = tds[0].getText()
            value = tds[1].getText()

            key = self.detail_mapping.get(title, None)
            if key:
                issue_table[key] = value
            else:
                self.logger.info(u'undefined %s' % title)

        return {'issue': issue_table}

    def save(self, data):
        data['updateTime'] = now()
        for key, value in data.items():
            print '{}: {}'.format(key, value)
        print '='*70

    def deal(self, tp, soup):
        super(CompanyIssue, self).deal(tp=tp, soup=soup)

    def run(self, startType=1, endType=4): #由于港股没有issue信息,因此startType和endType只能从1-4中取值
        super(CompanyIssue, self).run(startType=startType, endType=endType)

def main():
    global CATEGORY
    global DETAIL_MAPPING
    from_company_info = CompanyInfoUtil(category=CATEGORY, detail_mapping=DETAIL_MAPPING)
    from_company_info.set_logger(get_logger(__file__))

    try:
        CompanyIssue(from_company_info).run()
    except:
        from_company_info.logger.warn('%s shuttdown' % from_company_info.category)

if __name__ == '__main__':
    main()

